package com.haofengsoft.scce.ui.consultation;

import android.content.Context;
import android.os.Bundle;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.ImageView;
import android.widget.Toast;
import com.haofengsoft.platform.companent.pulltorefresh.PullToRefreshBase;
import com.haofengsoft.platform.companent.pulltorefresh.PullToRefreshListView;
import com.haofengsoft.scce.R;
import com.haofengsoft.scce.adapter.information.ZhongNanAdapter;
import com.haofengsoft.scce.adapter.information.ZhongNanModel;
import com.haofengsoft.scce.common.client.VolleyBean;
import com.haofengsoft.scce.common.client.VolleyInterface;
import com.haofengsoft.scce.common.client.VolleyManage;
import com.haofengsoft.scce.common.config.ConstantConfig;
import com.haofengsoft.scce.controller.consultation.ZhongNanController;
import com.haofengsoft.scce.ui.fragment.BaseFragment;

import java.util.HashMap;
import java.util.List;
import java.util.Map;

public class StampZhongNanFragment extends BaseFragment {

    private PullToRefreshListView zhongnanPtrl;
    private ZhongNanAdapter zhongnanAdapter = null;

    private int pageIndex = 0;
    private Context context;


    public static StampZhongNanFragment newInstance(String key) {
        Bundle bundle = new Bundle();
        bundle.putString("key", key);
        StampZhongNanFragment fragment = new StampZhongNanFragment();
        fragment.setArguments(bundle);
        return fragment;
    }

    public View onCreateView(LayoutInflater inflater, ViewGroup container, Bundle savedInstanceState) {

        View view = inflater.inflate(R.layout.consultation_stamp_zhongnan, container, false);
        view.setOnTouchListener(new DontSpillOnTouchListener());
        context = getActivity();
        view.setOnTouchListener(new DontSpillOnTouchListener());
        zhongnanPtrl = (PullToRefreshListView) view.findViewById(R.id.consultation_zhong_nan_ptrl);
        zhongnanPtrl.setMode(PullToRefreshBase.Mode.DISABLED);
        ImageView nodataIv = (ImageView) view.findViewById(R.id.zhong_nan_no_data_iv);
        nodataIv.setOnClickListener(new DataLoadOnClickListener());
        zhongnanPtrl.setEmptyView(nodataIv);
        zhongnanPtrl.setOnFocusChangeListener(null);
        zhongnanPtrl.setOnHierarchyChangeListener(null);
        zhongnanPtrl.setOnRefreshListener(new LoadPullData());

        initVolleyData();

        return view;
    }

    class DataLoadOnClickListener implements View.OnClickListener {

        @Override
        public void onClick(View view) {
            initVolleyData();
        }
    }

    class LoadPullData implements PullToRefreshBase.OnRefreshListener2 {
        // 下拉(手势下拉)
        @Override
        public void onPullDownToRefresh(PullToRefreshBase refreshView) {
            pageIndex = 0;
            zhongnanAdapter = null;
            initVolleyData();
        }

        @Override
        public void onPullUpToRefresh(PullToRefreshBase refreshView) {
            initVolleyData();
        }
    }

    private void refreshList(List<ZhongNanModel> modelList) {
        if (null != modelList) {
            if (null == zhongnanAdapter) {
                zhongnanAdapter = new ZhongNanAdapter(context, modelList);
                zhongnanPtrl.setAdapter(zhongnanAdapter);
            } else {
                zhongnanAdapter.addZhongNanList(modelList);
            }
            pageIndex = pageIndex + 1;
            zhongnanAdapter.notifyDataSetChanged();
        } else {
            Toast.makeText(getActivity(), "没有信息了", Toast.LENGTH_SHORT).show();
        }
        zhongnanPtrl.onRefreshComplete();
    }

    private void initVolleyData() {
        Map<String, Object> mapParam = new HashMap<String, Object>();
        mapParam.put("PageIndex", pageIndex);
        mapParam.put("PageSize", 10);
        mapParam.put("TypeID", 4);

        VolleyManage.getInstance().methodPost(getActivity(), null, ConstantConfig.TYPE_NEWS_URL, mapParam,
                new ZhongNanVolleyReal());
    }

    @Override
    protected void lazyLoad() {

    }

    class ZhongNanVolleyReal implements VolleyInterface {
        @Override
        public void gainData(VolleyBean bean) {
            if (bean.isSuccess()) {
                List<ZhongNanModel> modelList = ZhongNanController.getInstance(getActivity()).handlesZhongNanModelList(
                        bean.getContent());
                refreshList(modelList);
            } else {
                Toast.makeText(getActivity(), "网络连接错误", Toast.LENGTH_SHORT).show();
            }
        }
    }

}
